package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Special;
import com.entity.Data.PagerData;
import com.entity.Query.SpecialQuery;
import com.service.SingerService;
import com.service.SpecialService;
import com.util.MyUtil;

/**
 * 专辑
 * @author Shinelon
 *
 */
@RequestMapping("/special")
@Controller
public class SpecialController {

	
	@Autowired
	private SpecialService specialService;
	@Autowired
	private SingerService singerService;
	
	@RequestMapping("/special_show")
	public String special_show(Model model,SpecialQuery specialQuery,Integer curPage) {
		//如果没有页数，则页数默认为1
		if(curPage==null){
			curPage = 1;
		}		
		
		PagerData specialList = specialService.queryByPage(specialQuery, curPage, 5);
		model.addAttribute("specialList", specialList);
		
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/special";
	}
	
	@RequestMapping("/special_add_show")
	public String special_add_show(Model model) {
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/special-add";
	}
	
	@RequestMapping("/special_add_submit")
	public String special_add_submit(@RequestParam(required=false) MultipartFile[] wj,HttpServletRequest request) {
		Special special = new Special();
		//获取保存文件的服务器目录
		String saveFilePath = request.getServletContext().getRealPath("myfile/special/");
		//遍历每个上传文件进行保存
		for(MultipartFile mf : wj){
			//文件名
			String oldFileName = mf.getOriginalFilename();
			//扩展名
			String ext = FilenameUtils.getExtension(oldFileName);
			System.out.println(oldFileName+" "+ext);
			//新的文件名
			String newFileName = UUID.randomUUID()+"_"+oldFileName;
			//构建要保存的文件对象
			File f=new File(saveFilePath,newFileName);
			if(!f.exists()){
				f.mkdirs();
			}
			//将上传文件存到指定的文件对象上
			try {
				mf.transferTo(f);
				if("png".equals(ext)){
					special.setSpecialPhoto("myfile/special/"+newFileName);
				}else if("jpg".equals(ext)){
					special.setSpecialPhoto("myfile/special/"+newFileName);
				}
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String specialName = request.getParameter("specialName");
		String specialType = request.getParameter("specialType");
		String language = request.getParameter("language");
		String issueDate = request.getParameter("issueDate");
		String recordCompany = request.getParameter("recordCompany");
		String specialIntro = request.getParameter("specialIntro");
		String singerId = request.getParameter("singerId");
		
		special.setSpecialName(specialName);
		special.setSpecialType(specialType);
		special.setLanguage(language);
		special.setIssueDate(MyUtil.getDate(issueDate));
		special.setRecordCompany(recordCompany);
		special.setSpecialIntro(specialIntro);
		special.setSingerId(Integer.parseInt(singerId));
		specialService.addSpecial(special);
		return "admin/special-add";
	}
	
	@RequestMapping("/special_update_show")
	public String special_update_show(Integer specialId,Model model) {
		Map<String, Object> specialMap = specialService.querySpecialId(specialId);
		model.addAttribute("specialMap", specialMap);
		List<Map<String, Object>> singerList = singerService.querySinger();
		model.addAttribute("singerList", singerList);
		return "admin/special-edit";
	}
	
	@RequestMapping("/special_update_submit")
	public String special_update_submit(Special special) {
		specialService.editSpecial(special);
		return "admin/special-edit";
	}
	
	@RequestMapping("/special_delete")
	public String special_delete(Integer specialId) {
		specialService.delSpecial(specialId);
		return "admin/special";
	}
}
